package com.gaj.util;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

public class XmlCount {
    private static HashSet<String> jieDianSet = new HashSet<>();
    private static HashSet<String> secondLastLevelNodes = new HashSet<>();

    public static void main(String[] args) {
        String filePath = "D:\\idea_projects\\gajSpringBoot\\doc\\BJCM政法智能办案平台中心平台项目技术接口标准V4.5.4.2\\标准XML文件下发版\\11_一审公诉_bjcm";

        File file = new File(filePath);
        readXml(file);

        // 输出倒数第二层节点
        //System.out.println("倒数第二层节点:");
        for (String nodeName : secondLastLevelNodes) {
            System.out.print(nodeName + " ");
        }
        System.out.println();
    }

    //读取xml文件
    public static void readXml(File file) {
        SAXReader reader = new SAXReader();
        try {
            File[] listFiles = file.listFiles();
            for (File listFile : listFiles) {
                Document document = reader.read(listFile);
                Element rootElement = document.getRootElement();
                traverseXml(rootElement);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //递归遍历xml文件, 统计非叶子节点数量
    public static void countNonLeafNodes(Element element) {
        if (!isLeafNode(element)) {
            jieDianSet.add(element.getName());
        }
    }

    //递归遍历
    @SuppressWarnings("unchecked")
    public static void traverseXml(Element element) {
        List<Element> elements = element.elements();
        for (Element child : elements) {
            countNonLeafNodes(child);
            findSecondLastLevelNodes(child);
        }
    }

    // 筛选倒数第二层节点
    @SuppressWarnings("unchecked")
    public static void findSecondLastLevelNodes(Element element) {
        List<Element> children = element.elements();

        // 如果当前节点有子节点
        if (!children.isEmpty()) {
            // 检查所有子节点是否都是叶子节点
            boolean allChildrenAreLeaves = true;
            for (Element child : children) {
                if (!isLeafNode(child)) {
                    allChildrenAreLeaves = false;
                    break;
                }
            }

            // 如果所有子节点都是叶子节点，则当前节点是倒数第二层
            if (allChildrenAreLeaves) {
                secondLastLevelNodes.add(element.getName());
            }

            // 继续递归遍历子节点
            for (Element child : children) {
                findSecondLastLevelNodes(child);
            }
        }
    }

    //叶子节点
    public static boolean isLeafNode(Element element) {
        return element.elements().isEmpty();
    }

    //保留文件名中包含"看守所"的xml, 其它删除
    public static void keepSpecifiedXml(File file) {
        assert file.isDirectory();
        for (File childFile : Objects.requireNonNull(file.listFiles())) {
            for(File xmlFile : Objects.requireNonNull(childFile.listFiles())) {
                if(!xmlFile.getName().contains("看守所")) {
                    xmlFile.delete();
                    //System.out.println("删除文件: " + xmlFile.getName());
                }
            }
        }
    }

    //读取xml数量
    public static void xmlCount(File file) {
        int xmlCount = 0;

        File[] files = file.listFiles();
        assert files != null;
        for (File f : files) {
            if (f.isDirectory()) {
                File[] xmlFiles = f.listFiles();
                assert xmlFiles != null;
                for (File xmlFile : xmlFiles) {
                    System.out.println(xmlFile.getName());
                    xmlCount++;
                }
            }
        }
        System.out.println("xmlCount = " + xmlCount);//347个xml文档
    }
}
